package com.wph.gulimall.coupon.service.impl;

import com.wph.common.to.MemberPrice;
import com.wph.common.to.SkuReductionTo;
import com.wph.gulimall.coupon.entity.MemberPriceEntity;
import com.wph.gulimall.coupon.entity.SkuLadderEntity;
import com.wph.gulimall.coupon.service.MemberPriceService;
import com.wph.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wph.common.utils.PageUtils;
import com.wph.common.utils.Query;

import com.wph.gulimall.coupon.dao.SkuFullReductionDao;
import com.wph.gulimall.coupon.entity.SkuFullReductionEntity;
import com.wph.gulimall.coupon.service.SkuFullReductionService;

import javax.annotation.Resource;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    SkuLadderService skuLadderService;

    @Resource
    MemberPriceService memberPriceService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo reductionTo) {
        //5.4、保存sku的优惠、满减等信息    `gulimall_sms`------这个涉及到跨数据库操作了  `sms_sku_ladder`---打折表（买多少件减多少前 阶梯价格）
        //`sms_sku_full_reduction` 满多少价减多少前       `sms_member_price`---会员价表
        //1）、保存满减打折、会员价
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(reductionTo.getSkuId());
        skuLadderEntity.setFullCount(reductionTo.getFullCount());//满几件  可以去实体类中和数据库中查找到相关的信息
        skuLadderEntity.setDiscount(reductionTo.getDiscount());//打几折
        skuLadderEntity.setAddOther(reductionTo.getCountStatus());//是否可以叠加其他优惠
        //skuLadderEntity还有一个折后价格这里没设置，在这里可以设置，也可以在下订单的时候在计算折后价格
        if (reductionTo.getFullCount() > 0){
            skuLadderService.save(skuLadderEntity);
        }

        //2）、`sms_sku_full_reduction` 满多少价减多少前
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(reductionTo,skuFullReductionEntity);
        //skuFullReductionEntity.setAddOther(reductionTo.getFullCount());  //这里是否少了叠加优惠
        if (reductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 0){
            this.save(skuFullReductionEntity);
        }

        //3)、`sms_member_price`---会员价表
        List<MemberPrice> memberPrices = reductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrices.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(reductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(item->{
            return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);


    }
}